<?php
namespace plugins\upload\model;
/**=========================================
 * Copyright © 2023-2024, WICMS.CN. All Rights Reserved
 * Project Name: WiCMS
 * Description: 附件模型
 * Author: jason
 * Email: 52527998@QQ.COM
 * Date: 2024/12/31
 * Time: 16:41
 * File: plugins\upload\model\AttachmentModel.php
 * =========================================
 */

use think\Model;

class AttachmentModel extends Model
{

    protected $pk                 = 'id';
    protected $name               = 'Attachment';
    protected $autoWriteTimestamp = true;
    protected $createTime         = 'create_time';
    protected $updateTime         = 'update_time';

    public static function deleteAttachment(string $avatar):bool
    {
        // 取文件名称
        $fileName = basename($avatar);
        // 查找文件信息
        $fileData = self::where('file_name', $avatar)->findOrEmpty();
        if (!$fileData->isEmpty()) {
            //组装文件路径
            $filePath = public_path('uploads/' . $fileData->file_path . '/' . $fileName);
            if (file_exists($filePath)) {
                // 删除文件
                unlink($filePath);
                // 删除数据库记录
                $fileData->delete();
            }
        }
        return true;
    }

}